package des.algorithm;

import des.output.Output;

public class TripleDES extends Encryption {

	private DES des;
	private Key[] keys;

	public TripleDES(long originalKey0, long originalKey1) {
		des = new DES();
		keys = new Key[2];
		keys[0] = new Key(originalKey0);
		keys[1] = new Key(originalKey1);
	}

	public long encryptLong(long bits) {
		Output.printTitle("3DES Encoding process:");
		status = Status.ENCRYPTING;
		return mainProcess(bits);
	}

	public long decryptLong(long bits) {
		Output.printTitle("3DES Decoding process:");
		status = Status.DECRYPTING;
		return mainProcess(bits);
	}

	private long mainProcess(long bits) {
		if (status == Status.ENCRYPTING) {
			bits = des.encodeLong(bits, keys[0]);
			bits = des.decodeLong(bits, keys[1]);
			bits = des.encodeLong(bits, keys[0]);
		} else {
			bits = des.decodeLong(bits, keys[0]);
			bits = des.encodeLong(bits, keys[1]);
			bits = des.decodeLong(bits, keys[0]);
		}

		return bits;
	}
}
